var block_html;
var method_html = "";
var addMember;
var addIcCard;
$(function () {

	//获取配置的访问方式
	$.ajax({
		url: "/basicVisitorMethod/selTypeList",
		data: {	},
		dataType: 'json',
		type: 'post',
		async:false,
		success: function(data){
			if(data.resultCode === '000'){
				var content = data.resultContent;
				for(var i in content){
					method_html += '<input type="checkbox"  name="like" value="'+ content[i].visitorMethodKey +'" title="'+ content[i].visitorMethodName + ' "  >';
				}
			}
		},
		error : function(){
			alert('获取访问方式失败');
		}
	});

});

function getRoomByBid( bid ){
	var room_html;
	$.ajax({
		url: "/basicRoom/selRoomByBlock",
		data: {	blockId: bid},
		dataType: 'json',
		type: 'post',
		async:false,
		success: function(data){
			if(data.resultCode === '000'){
				var content = data.resultContent;
				var com_html = "";
				for(var i in content){
					com_html += '<option value="'+ content[i].roomId +'"  >'+content[i].roomName +'</option>';
				}
				room_html = com_html;
				// $('#s_user_room_id').html(com_html );
			}
		},
		error : function(){
			alert('获取房间信息失败');
		}
	});
	return room_html;
}

layui.use(['element','table','layer','form','laydate'], function(){
	var table = layui.table;
	var layer = layui.layer;
	var form=layui.form
		, $ = layui.jquery;
	util = layui.util;
	var laydate = layui.laydate;

	laydate.render({
		elem: '#time',
		trigger:'click'//解决一闪而过的问题
	});
	loadBulidingOptions();
	table.render({
		elem: '#basicUserTable',
		height: 'full-150',
		url:  "/basicUser/userTable", //数据接口
		method: 'post',
		request:{
			page: 'pageNum', //页码的参数名称，默认：page
			limit: 'pageSize' //每页数据量的参数名，默认：limit
		},
		where: {community: $(window.parent.document).find("#community").val()},
		page: true, //开启分页
		cols: [[ //表头
			{type:'numbers'}
			,{field: 'userId', title: 'ID',hide:true,align:'center' }
			,{field: 'userType', title: '人员类型',width:100,align:'center' ,templet: function (d) {
					return userType(d.userType);
				} }
			,{field: 'userName', title: '姓名',width:100,align:'center' }
			,{field: 'userCardType', title: '证件类型',width:100, align:'center',templet: function (d) {
					return cardType(d.userCardType);
				} }
			,{field: 'userCard', title: '证件id',width:120, align:'center'  }
			,{field: 'userPhone', title: '电话',width:120, align:'center'  }
			// ,{field: 'userRoomId', title: '房间ID',width:120, align:'center'  }
			,{field: 'userRoomName', title: '房间名称',width:120, align:'center'  }
			// ,{field: 'userBlockId', title: '楼栋ID',width:120, align:'center'  }
			,{field: 'userBlockName', title: '楼栋名称',width:120, align:'center'  }
			// ,{field: 'userDoorCard', title: '门禁卡信息',width:120, align:'center'  }
			// ,{field: 'userFingerprint', title: '指纹信息',width:120, align:'center'  }
			,{field: 'userFaceInfo', title: '脸部信息',width:120, align:'center',templet: function (d) {
					if (d.userFaceInfo) {
						// return '<a href="'+d.completePicUrl+'" target="_blank">查看</a>';
						return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="' + d.userFaceInfo + '"/>';
					}
					return "";
				}}
			,{field: 'createTime', title: '添加时间',width:170, align:'center',templet: function (d) {
					return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
				}    }
			, {field: 'right', title: '操作', align:'center',width:260, fixed: 'right',templet:function(data){
					var op = '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
					op+='<a class="layui-btn layui-btn-xs" lay-event="memberMgr">成员</a>';
					op+='<a class="layui-btn layui-btn-xs" lay-event="icCardMgr">IC卡</a>';
					op+='<a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>';
					return op;
				}
			}
		]]

	});

	function loadBulidingOptions(blockId){
		$.ajax({
			url: "/basicBlock/selBlockList",
			data: {community:$(window.parent.document).find("#community").val()},
			dataType: 'json',
			type: 'post',
			async:false,
			success: function(data){
				if(data.resultCode === '000'){
					var content = data.resultContent;
					var com_html = "";
					for(var i in content){
						com_html += '<option value="'+ content[i].blockId +'"  >'+content[i].blockName +'</option>';
					}
					block_html = com_html;
					$('#s_user_blockId').html("<option value=\"\" selected=\"\" >全部</option>"+block_html );
					if(blockId){
						$('#s_user_blockId').val(blockId);
					}
					form.render('select');
				}
			},
			error : function(){
				alert('获取楼栋信息失败');
			}
		});
	}

	//添加人员信息
	addUser = function () {
		loadBulidingOptions();
		$('#userFaceInfo').val('');
		$("#basic_up").hide();
		$("#basic_form").show();
		$("#basic_form").attr("disabled", false);
		$("#basic_form").attr("class", "layui-btn");
		$("#product_form")[0].reset();
		$('#userBlockId').html("<option value=\"\" selected=\"\" >--请选择--</option>"+block_html);
		$('#userId').val("");
		$('#demo1').removeAttr("src");
		layer.open({
			type: 1
			, title: '新增'
			, shadeClose: true
			, area: ['500px', '80%']
			, content: $('#userAdd')
		});
	};
	var addMemberDiv;
	addMember = function(){
		$("#memberModify").hide();
		$("#memberAdd").show();
		$("#memberAdd").attr("disabled", false);
		$("#memberAdd").attr("class", "layui-btn");
		$('#userFaceInfo2').val('');
		$("#memberAddForm")[0].reset();
		// 如果是业主11才显示户主关系选项
		if($("#userMType").val() === "11"){
			$("#familyRelationshipDiv").show();
		}else {
			$("#familyRelationshipDiv").hide();
		}
		$('#demo2').removeAttr("src");
		addMemberDiv = layer.open({
			type: 1
			, title: '新增家庭成员'
			, shadeClose: true
			, area: ['35%', '80%']
			, content: $('#memberAddDiv')
		});
	};
	var addIcCardDiv;
	addIcCard = function(){
		$("#ICCardAddForm")[0].reset();
		addIcCardDiv = layer.open({
			type: 1
			, title: '添加IC卡'
			, shadeClose: true
			, area: ['35%', '60%']
			, content: $('#ICCardAddDiv')
		});
	};

	//操作列
	table.on('tool(basicUserTable)', function(obj){
		var data = obj.data;
		switch (obj.event) {
			case 'delete':
				layer.confirm('是否确认删除'+data.userName+'的信息？', {
					btn: ['确认', '返回'] //按钮
				}, function () {
					layer.load();
					var companyId = $(window.parent.document).find("#community").val();
					var params = {bid:data.userId,companyId:companyId,fafId:data.userFingerprint}
					$.post('/basicUser/delUser',params,function (res) {
						if(res.resultCode === '000'){
							layer.msg('删除成功',{
								time:500,
								end:function () {
									reloadUserList();
								}
							})
						}else{
							layer.msg(res.resultDesc);
						}
						layer.closeAll();
					})
				});
				break;
			case 'edit':
				loadBulidingOptions();
				$('#userFaceInfo').val('');
				$("#basic_up").show();
				$("#basic_form").hide();
				$("#basic_up").attr("disabled", false);
				$("#basic_up").attr("class", "layui-btn");
				$("#userId").val(data.userId);
				$("#userAge").val(getAgeByIdCard(data.userCard));
				$("#fafId").val(data.userFingerprint);//人脸照片id
				form.val('product_form',{
					// 'userId': data.userId, // 隐藏元素这样赋值有问题
					'userRoomName': data.userRoomName,
					'userBlockName':data.userBlockName,
					'userName':data.userName,
					'userPhone': data.userPhone,
					'userCard': data.userCard,
					'userDoorCard': data.userDoorCard
				});
				$('#userBlockId').html("<option value=\"\" selected=\"\" >--请选择--</option>"+block_html);
				form.render();
				var select = 'dd[lay-value=' + data.userBlockId + ']';
				$('#userBlockId').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.userRoomId + ']';
				$('#userRoomId').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.gender + ']';
				$('#gender').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.userCardType + ']';
				$('#userCardType').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.userType + ']';
				$('#userType').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.hushaiConsistent + ']';
				$('#hushaiConsistent').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.partyMembership + ']';
				$('#partyMembership').siblings("div.layui-form-select").find('dl').find(select).click();
				form.render();
				if(data.userFaceInfo === null || data.userFaceInfo==="" ){
					$('#demo1').removeAttr("src");
				}else {
					$('#demo1').attr('src', data.userFaceInfo);
				}
				layer.open({
					type: 1
					, title: '编辑业主/租户信息'
					, shadeClose: true
					, area: ['500px', '80%']
					, content: $('#userAdd')
				});
				break;
			case 'editVisitor':
				$('#plug_sel_list').html(method_html );
				$('#uid').val(data.userId);
				$.ajax({
					url: "/basicUserMethod/selUserTypeList",
					data: {  user : data.userId  },
					dataType: 'json',
					type: 'post',
					async: false,
					success: function(data){
						if(data.resultCode === '000'){
							var content = data.resultContent;
							for(var i in content){
								$ ('input[value='+content[i].visitorMethodKey+']' ).prop("checked",true );
							}
						}
					},
					error : function(){
						alert('获取版本的插件信息失败');
					}
				});
				form.render();
				layer.open({
					type: 1
					, title: '管理访问方式'
					, shadeClose: true
					, area: ['55%', '80%']
					, content: $('#visitType')
				});
				break;
			case 'memberMgr':
				memberMgr(data);
				break;
			case 'icCardMgr':
				icCardMgr(data);
				break;
		}

	});

	function memberMgr(data){
		$("#memberBlockId").val(data.userBlockId);
		$("#memberCompanyId").val(data.companyId);
		$("#memberParentId").val(data.userId);
		$("#memberRoomId").val(data.userRoomId);
		$("#memberRoomName").val(data.userRoomName);
		$("#memberBlockName").val(data.userBlockName);
		table.render({
			elem: '#memberUserTable'
			,url:'/basicUser/memberTable'
			,where:{parentId: data.userId,companyId: $(window.parent.document).find("#community").val()}
			,method:'get'
			,toolbar: '#toolbarMember'
			,title: '家庭成员'
			,cols: [[
				{type:'numbers',title:'序号'}
				,{field:'userName', title:'姓名', }
				,{field:'userPhone', title:'手机号', }
				,{field:'familyRelationship', title:'户主关系', templet:function(d){
						return familyRelation(d.familyRelationship);
					}}
				,{field:'state',align : 'center',title:'审批状态',templet:function(d){
						return parseState(d.state);
					}}
				,{fixed: 'right', title:'操作',align : 'center', templet:function(data){
						var op = '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
						op+='<a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>';
						return op;
					}}
			]]
			,page: false
		});
		$("#userMType").val(data.userType);
		layer.open({
			type: 1
			, title: '家庭成员管理'
			, shadeClose: true
			, area: ['80%', '80%']
			, content: $('#familyMemberDiv')
		});
	}
	function icCardMgr(data){
		$("#cardId").val(data.cardId);
		$("#icCardUserId").val(data.userId);
		$("#icCardCompanyId").val($(window.parent.document).find("#community").val());
		table.render({
			elem: '#icCardTable'
			,url:'/basicUser/icCard/list'
			,where:{userType :2,userId:data.userId,companyId: $(window.parent.document).find("#community").val()}
			,method:'get'
			,toolbar: '#toolbarIcCard'
			,title: 'IC卡管理'
			,cols: [[
				{type:'numbers',title:'序号'}
				,{field:'cardId', title:'卡号id',hide:true }
				,{field:'cardNO', title:'卡号', }
				,{field:'identifier', title:'编号', }
				,{field:'time', title:'过期日期', }
				,{field:'createTime', title:'创建日期', templet:function(d){
						return util.toDateString(d.createTime, "yyyy-MM-dd");
					}}
				,{field:'cardState',align : 'center',title:'状态',templet:function(d){
						return parseIcCardState(d.cardState);
					}}
				,{fixed: 'right', title:'操作',align : 'center', templet:function(data){
						var op = '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
						op+='<a class="layui-btn layui-btn-xs" lay-event="active">启用</a>';
						op+='<a class="layui-btn layui-btn-xs" lay-event="cancel">注销</a>';
						return op;
					}}
			]]
			,page: false
		});
		layer.open({
			type: 1
			, title: 'IC卡管理'
			, shadeClose: true
			, area: ['80%', '80%']
			, content: $('#icCardDiv')
		});
	}

	var editMemberDiv;
	table.on('tool(memberUserTable)', function(obj){
		var data = obj.data;
		switch (obj.event) {
			case 'delete':
				layer.confirm('是否确认删除'+data.userName+'的信息？', {
					btn: ['确认', '返回'] //按钮
				}, function () {
					var index = layer.load(2, {
						shade: [0.4, '#000'],
						content: '提交中...',
						success: function (layero) {
							layero.find('.layui-layer-content').css({
								'padding-top': '40px',
								'width': '200px'
							});
						}
					});
					$.post('/basicUser/delMember',{uid:data.userId,companyId:data.companyId},function (res) {
						if(res.resultCode === '000'){
							layer.msg('删除成功',{
								time:500,
								end:function () {
									reloadMemberList();
								}
							})
						}else{
							layer.msg(res.resultDesc);
						}
						layer.close(index);
					})
				});
				break;
			case 'edit':
				$("#memberModify").show();
				$("#memberAdd").hide();
				$("#memberAdd").attr("disabled", false);
				$("#memberAdd").attr("class", "layui-btn");
				$("#memberRoomId").val(data.userRoomId);
				$("#memberRoomName").val(data.userRoomName);
				$("#memberBlockName").val(data.userBlockName);
				$("#memberUserId").val(data.userId);
				$("#memberBlockId").val(data.userBlockId);
				$("#memberCompanyId").val(data.companyId);
				$("#memberParentId").val(data.parentId);
				$("#userAge2").val(getAgeByIdCard(data.userCard));
				$("#fafId2").val(data.userFingerprint);//人脸照片id
                $('#userFaceInfo2').val('');
				form.val('memberAddForm',{
					// 'userRoomId': data.userRoomId,
					// 'userRoomName': data.userRoomName,
					// 'userBlockName': data.userBlockName,
					// 'userId': data.userId,
					// 'companyId': data.companyId,
					// 'parentId': data.parentId,
					// 'userBlockId': data.userBlockId,
					'userName':data.userName,
					'userPhone': data.userPhone,
					'userCard': data.userCard,
					'userDoorCard': data.userDoorCard
				});
				form.render();
				var select = 'dd[lay-value=' + data.gender + ']';
				$('#memberGender').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.hushaiConsistent + ']';
				$('#hushaiConsistent2').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.familyRelationship + ']';
				$('#familyRelationship2').siblings("div.layui-form-select").find('dl').find(select).click();
				select = 'dd[lay-value=' + data.partyMembership + ']';
				$('#partyMembership2').siblings("div.layui-form-select").find('dl').find(select).click();
				form.render();
				if(data.userFaceInfo === null || data.userFaceInfo==="" ){
					$('#demo2').removeAttr("src");
				}else {
					$('#demo2').attr('src', data.userFaceInfo);
				}
				// 如果是业主11才显示户主关系选项
				if($("#userMType").val() === "11"){
					$("#familyRelationshipDiv").show();
				}else {
					$("#familyRelationshipDiv").hide();
				}
				editMemberDiv = layer.open({
					type: 1
					, title: '编辑家庭成员信息'
					, shadeClose: true
					, area: ['35%', '80%']
					, content: $('#memberAddDiv')
				});
				break;
		}

	});
	var ICCardEditDiv;
	table.on('tool(icCardTable)', function(obj){
		var data = obj.data;
		switch (obj.event) {
			case 'active':
				layer.confirm('是否启用？', {
					btn: ['确认', '返回'] //按钮
				}, function () {
					var params = {cardId:data.cardId,cardState:0,companyId:$(window.parent.document).find("#community").val()};
					cardState(params);
				});
				break;
			case 'edit':
				$("#cardId").val(data.cardId);
				$("#icCardUserId").val(data.userId);
				$("#icCardCompanyId").val($(window.parent.document).find("#community").val());
				$("#cardNO").val(data.cardNO);
				$("#identifier").val(data.identifier);
				$("#time").val(data.time);
				ICCardEditDiv = layer.open({
					type: 1
					, title: '编辑信息'
					, shadeClose: true
					, area: ['30%', '60%']
					, content: $('#ICCardAddDiv')
				});
				break;

			case 'cancel':
				layer.confirm('是否确认注销？', {
					btn: ['确认', '返回'] //按钮
				}, function () {
					var params = {cardId:data.cardId,cardState:2,companyId:$(window.parent.document).find("#community").val()};
					cardState(params);
				});
				break;
		}

	});

	function cardState(params){
		$.post('/basicUser/icCard/cardStatus',params,function (res) {
			if(res.resultCode === '000'){
				layer.msg('操作成功',{
					time:500,
					end:function () {
						reloadIcCardList();
					}
				})
			}else{
				layer.msg(res.resultDesc);
			}
		});
	}
	// 身份证输入框失去焦点显示年龄
	$("#userCard").blur(function(){
		$("#userAge").val(getAgeByIdCard($(this).val()));
	});
	$("#userCard2").blur(function(){
		$("#userAge2").val(getAgeByIdCard($(this).val()));
	});

	//更新人员信息
	form.on('submit(basic_up)',function(data){
		$("#basic_up").attr("disabled", true);
		$("#basic_up").attr("class", "layui-btn layui-btn-disabled");
		var inFun = 0;
		var formData = new FormData($("#product_form")[0]);
		var userCard = $.trim($("#userCard").val());
		if(userCard){
			if(!SFID(userCard)){
				layer.msg("身份证号码有误",{icon:5});
				return false;
			}
		}
		layer.confirm('是否确认提交人员信息？', {
			btn: ['确认', '返回'] //按钮
		}, function () {
			if(inFun === 1){
				return;
			}
			inFun = 1;
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '提交中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			formData.append("file",$("#uploadImg")[0].files[0]);
			formData.append("companyId",$(window.parent.document).find("#community").val());
			// 如果是租客则增加FAMILY_RELATIONSHIP为4租客
			// if($("#userType").val() === "13"){
			// 	formData.append("familyRelationship","4");
			// }
			$.ajax({
				url: '/basicUser/addUser',
				type: 'POST',
				data: formData,
				contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作，从而失去分界符，而使服务器不能正常解析文件
				processData: false,
				success:function(res){
					if (res.resultCode === '000'){
						layer.closeAll();
						layer.msg('提交成功',{
							time:500,
							end:function () {
								reloadUserList();
							}
						})

					}else {
						layer.close(index);
						layer.msg( res.resultDesc );
						$("#basic_up").attr("disabled", false);
						$("#basic_up").attr("class", "layui-btn");
					}
				},
				done: function () {
					$("#gateway_form")[0].reset();
					$("#basic_up").attr("disabled", false);
					$("#basic_up").attr("class", "layui-btn");
				}
			});
		});
		return false;//返回false 防止二次提交
	});

	// 新增成员
	form.on('submit(memberSubmit)',function(data){
		var inFun = 0;
		var formData = new FormData($("#memberAddForm")[0]);
		var userCard = $.trim($("#userCard2").val());
		if(userCard){
			if(!SFID(userCard)){
				layer.msg("身份证号码有误",{icon:5});
				return false;
			}
		}
		layer.confirm('是否确认提交人员信息？', {
			btn: ['确认', '返回']
		}, function () {
			if(inFun === 1){
				return;
			}
			inFun = 1;
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '提交中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			formData.append("file",$("#uploadImg2")[0].files[0]);
			$.ajax({
				url: '/basicUser/addMember',
				type: 'POST',
				data: formData,
				contentType: false,
				processData: false,
				success:function(res){
					if (res.resultCode === '000'){
						layer.msg('提交成功',{
							time:500,
							end:function () {
								reloadMemberList();
							}
						});
					}else {
						layer.msg(res.resultDesc);
					}
					layer.close(index);
					if(addMemberDiv){
						layer.close(addMemberDiv);
					}
					if(editMemberDiv){
						layer.close(editMemberDiv);
					}
				},
				done: function () {
					$("#memberAddForm")[0].reset();
				}
			});
		});
		return false;
	});

	// 添加ic卡
	form.on('submit(iCCardAdd)',function(data){
		var inFun = 0;
		layer.confirm('确认添加IC卡？', {
			btn: ['确认', '返回']
		}, function () {
			if(inFun === 1){
				return;
			}
			inFun = 1;
			$.ajax({
				url: '/basicUser/icCard/addOrEdit',
				type: 'post',
				data: data.field,
				async: false,
				success:function(res){
					if (res.resultCode === '000'){
						layer.msg('提交成功',{
							time:500,
							end:function () {
								reloadIcCardList();
							}
						})

					}else {
						layer.msg(res.resultDesc );
					}
					if(addIcCardDiv){
						layer.close(addIcCardDiv);
					}
					if(ICCardEditDiv){
						layer.close(ICCardEditDiv);
					}
				},
				done: function () {
					$("#ICCardAddForm")[0].reset();
				}
			});
		}, function () {
			layer.closeAll();
			return false;
		});
		return false;
	});

	function reloadUserList(){
		table.reload('basicUserTable', {
			page: {
				curr: 1 //重新从第 1 页开始
			}
			,where: {
				userType: $('#s_user_type').val(),
				blockID: $('#s_user_blockId').val(),
				roomId: $('#s_user_room_id').val(),
				userName: $('#searchBlockName_user').val(),
				community: $(window.parent.document).find("#community").val()
			}
		});
	}
	function reloadMemberList(){
		table.reload('memberUserTable', {});
	}
	function reloadIcCardList(){
		table.reload('icCardTable', {
			page: {
				curr: 1
			}
			,where: {
				userType: 2,
				userId: $('#icCardUserId').val(),
				community: $(window.parent.document).find("#community").val()
			}
		});
	}
	//查询人员信息
	form.on('submit(search_user)',function () {
		//执行重载
		var blockId = $('#s_user_blockId').val()
		loadBulidingOptions(blockId);
		reloadUserList();
		return false;
	});

	//检测blockId 选择的变化
	form.on('select(s_user_blockId)', function (data) {
		$('#s_user_room_id').html("<option value=\"\" selected=\"\" >全部</option>"+getRoomByBid(data.value) ) ;
		form.render();
	});

	form.on('select(userBlockId)', function (data) {
		$('#userRoomId').html("<option value=\"\" selected=\"\" >--请选择--</option>"+getRoomByBid(data.value) ) ;
		form.render();
		$('#userBlockName').val($("#userBlockId").find("option:selected").text());
	});

	form.on('select(userRoomId)', function ( ) {
		var userRoomName = $("#userBlockId").find("option:selected").text()+"-"+$("#userRoomId").find("option:selected").text()
		$('#userRoomName').val(userRoomName);
	});

	// 提交方式配置信息
	form.on('submit(sub_plug)',function( data){
		var arr_box = [];
		$('input[name=like]:checked').each(function() {
			arr_box.push($(this).val());
		});
		$.ajax({
			url: '/basicUserMethod/upTypeList',
			type: 'POST',
			data: {keys: JSON.stringify(arr_box), uid:data.field.uid  },
			success:function(res){
				layer.closeAll();
				if (res.resultCode === '000'){
					layer.msg('配置成功!',{
						time:500
					});
				}else {
					layer.msg(res.resultDesc,{
						time:2000
					});
				}
			}
		});
		return false;
	});
	// 同步数据
	$("#sync").on('click', function (){
		layer.confirm('同步数据耗时较长，是否继续？', {
			btn: ['确认', '取消'] //按钮
		}, function () {
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '同步中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			var companyId = $(window.parent.document).find("#community").val();
			$.post('/basicUser/syncData',{companyId:companyId},function (res) {
				if(res.resultCode === '000'){
					layer.msg(res.resultDesc,{
						time:500,
						end:function () {
							reloadUserList();
						}
					})
				}else{
					layer.msg(res.resultDesc);
				}
				layer.close(index);
			});
		})
	});
	// 导入
	$("#import").on('click', function (){
		layer.open({
			type: 1
			,title: '上传文件'
			,area: ['45%','30%']
			,content: $('#upload')
		});
	});

	// 导出
	$("#exportAll").on('click', function (){
		var communityId = $(window.parent.document).find("#community").val();
		if(communityId === ""){
			return layer.msg("小区id为空!",{icon:5});
		}
		downloadFile("/basicUser/exportUser2Excel?communityId="+communityId);
	});

	// 下载模板
	$("#down_user_template").on('click', function (){
		$('#downButton')[0].click();
	});
	//提交导入信息
	form.on('submit(upload_form)',function(){
		$("#upload_form").attr("disabled", true);
		$("#upload_form").attr("class", "layui-btn layui-btn-disabled");
		var formData = new FormData();
		formData.append("file",$("#uploadExcel")[0].files[0]);
		formData.append("community",$(window.parent.document).find("#community").val());
		layer.load();
		$.ajax({
			url:'/basicUser/uploadUser',
			type: 'POST',
			data: formData,
			processData: false,
			contentType:false,
			success:function(res){
				if (res.resultCode === "000") {
					reloadUser();
					layer.msg('导入成功!',{icon: 1});
				}else {
					layer.msg(res.resultDesc,{
						time: 5000,
					})
				}
				setTimeout(function () {
					layer.closeAll();
				},1000);
				$("#uploadExcel").val("");
				$("#file_name").val("");
				$("#upload_form").attr("disabled", false);
				$("#upload_form").attr("class", "layui-btn");
			}

		});
		return false;
	});

	$("#uploadProductBtn").on("click",function(){
		$("#uploadExcel").click();
	});
	$("#uploadExcel").on("change",function(){
		var name = $("#uploadExcel").val();
		var fileType = name.substring(name.lastIndexOf(".")+1);
		if(fileType !== "xls" && fileType !== "xlsx"){
			layer.msg("亲，请选择正确的excel格式哦!",{icon:5});
			$("#upload_form").attr("disabled", true);
			$("#upload_form").addClass("layui-btn-disabled");
		}else {
			$("#upload_form").attr("disabled", false);
			$("#upload_form").removeClass("layui-btn-disabled");
			var index = name.lastIndexOf("\\")+1;
			$("#file_name").val(name.substring(index));
		}
	});

	$("#uploadBtn").on("click",function(){
		$("#uploadImg").click();
	});
	$("#uploadBtn2").on("click",function(){
		$("#uploadImg2").click();
	});

	$("#uploadImg").on("change",function(){
		var filePath = $(this).val();
		if(filePath.toLowerCase().indexOf("jpg") !== -1 ) {
			var fr = new FileReader();//创建new FileReader()对象
			//判断图片大小
			var filesize = $(this)[0].files[0];
			if(filesize.size>0){
				if(filesize.size > 1024*500){
					layer.msg('请上传小于500K的图片');
					return false;
				}
			}
			fr.onload = function(e) {
				$('#demo1').attr('src', e.target.result);
				//$('#userFaceInfo').val(e.target.result);
			}
			var imgObj = this.files[0];//获取图片
			fr.readAsDataURL(imgObj);//将图片读取为DataURL

		} else {
			layer.msg("请上传不超过500kb的jpg格式的文件！",{icon:5});
			return false
		}
	});

	$("#uploadImg2").on("change",function(){
		var filePath = $(this).val();//读取图片路径
		if(filePath.toLowerCase().indexOf("jpg") !== -1 ) {
			var fr = new FileReader();//创建new FileReader()对象
			//判断图片大小
			var filesize = $(this)[0].files[0];
			if(filesize.size>0){
				if(filesize.size > 1024*500){
					layer.msg('请上传小于500K的图片');
					return false;
				}
			}
			fr.onload = function(e) {
				$('#demo2').attr('src', e.target.result);
				//$('#userFaceInfo').val(e.target.result);
			}
			var imgObj = this.files[0];//获取图片
			fr.readAsDataURL(imgObj);//将图片读取为DataURL
		} else {
			layer.msg("请上传不超过500kb的jpg格式的文件！",{icon:5});
			return false
		}
	});

	function getPreTypeByImgType(imgType){
		switch (imgType){
			case "png":
				return "data:image/png;base64,";
			case "jpg":
				return "data:image/jpeg;base64,";
			case "gif":
				return "data:image/gif;base64,";
			case "svg":
				return "data:image/svg+xml;base64,";
			case "ico":
				return "data:image/x-icon;base64,";
			case "bmp":
				return "data:image/bmp;base64,";
			default:
				return "";
		}
	}

});

function cardType(type ) {
	switch (type) {
		case '1':
			return '身份证';
		case '2':
			return '护照';
		case '3':
			return '驾照';
		default:
			return '未知类型';
	}
}

function userType(type ) {
	switch (type) {
		case '11': return '业主';
		case '12': return '家庭成员';
		case '13': return '租客';
		default:
			return '其他';
	}
}
function familyRelation(type) {
	switch (type) {
		case 0: return '父母';
		case 1: return '子女';
		case 2: return '夫妻';
		case 3: return '孙辈';
		case 4: return '亲属';
		case 5: return '朋友';
		case 6: return '祖父母';
		case 7: return '外祖父母';
		default:
			return '其他';
	}
}
function parseIcCardState(cardState) {
	switch (cardState) {
		case 0: return '启用';
		case 1: return '挂失';
		case 2: return '注销';
		default:
			return '-';
	}
}
function parseState(state) {
	switch (state) {
		case '0': return '未审核';
		case '1': return '通过';
		case '2': return '不通过';
		default:
			return '-';
	}
}

function reloadUser(){
	location.reload();
}

//根据身份证获取年龄
function getAgeByIdCard(userCard) {
	if(!userCard){
		return "";
	}
	var myDate = new Date();
	var month = myDate.getMonth() + 1;
	var day = myDate.getDate();
	var age = myDate.getFullYear() - userCard.substring(6, 10) - 1;
	if (userCard.substring(10, 12) < month || userCard.substring(10, 12) == month && userCard.substring(12, 14) <= day) {
		age++;
	}
	return age;
}
function magnifyPic(ele) {
	var width = $(ele).width();
	var height = $(ele).height();
	var scaleWH = width/height;
	var bigH = 450;
	var bigW = scaleWH*bigH;
	if(bigW>600){
		bigW = 600;
		bigH = bigW/scaleWH;
	}
	// 放大预览图片
	layer.open({
		type: 1,
		title: false,
		closeBtn: 1,
		shadeClose: true,
		area: [bigW + 'px', bigH + 'px'], //宽高
		content: "<img width='"+bigW+"' height='"+bigH+"' src=" + $(ele).attr("src") + " />"
	});
}